<template>
    <div>
        <span style="text-align: center;">密码登录 |短信登录</span>
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
            class="demo-ruleForm" :size="formSize" status-icon>
            <el-form-item label="手机号" prop="username">
                <el-input v-model="ruleForm.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)"
                    style="background: orange;width: 200px;  border: 1px solid orange ">
                    登录
                </el-button>
              <button  @click="hand">验证码</button><span></span>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import instance from '@/api/instance';
import router from '@/router';
import { reactive, ref } from 'vue'
router
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',

})
const rules = reactive({
    username: [
        { required: true, message: '请输入正确的手机号码', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ]
})
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            instance.post('login', ruleForm).then((res) => {
                if (res.status == 200 && res.data.code == 10000) {
                    localStorage.setItem('token', res.data.data.token)
                    router.push('/home')
                    ElMessage({
                        message: '登录成功',
                        type: 'success',
                    })
                }
            })
        } else {
            ElMessage({
                message: '登录失败',
                type: 'error',
            })
            console.log('error submit!', fields)
        }
    })
}
// 4.切到短信登录后显示两个输入框，手机号及获取验证码，击“获取验证码”按钮后，显示30秒倒计时。

const hand=()=>{
    console.log('获取验证码');
    let time =30;
    let timer =setInterval(()=>{
        time--;
        if(time<=0){
            {
            clearInterval(timer);
            time=30
        }
        console.log(timer);
        }
    })
}




</script>

<style lang="scss" scoped></style>